<?php
namespace App\Http\Controllers;

use App\Models\ActivityApply;
use App\Models\Zone;

use App\Http\Controllers\Controller;
use App\User;
use \DB;
use Auth;
use Validator;
use Illuminate\Support\Facades\Request;
use App\Http\Requests\ActivityProjectVideoPostRequest;
use App\Models\Activity;

class ActivityApplyController extends CommonController
{

    /**
     * 活动列表。
     *
     * @return Response
     */
    public function getIndex()
    {
        $datas = DB::table('activity_apply as aa')
            ->join("activity as a", "aa.activity_id", "=", "a.id")
            ->join("activity_type as at", "a.type", "=", "at.code")
            ->join("maker as m", "aa.maker_id", "=", "m.id")
            ->orderBy('aa.created_at','desc')
            ->select('aa.id','aa.sign_code', 'a.subject', 'aa.name', 'aa.tel', 'aa.created_at', 'aa.updated_at', 'at.name as type_name', 'm.name as maker_name')
            ->paginate(10);

        return view('activityapply.index')->with("datas", $datas)->with("actions", $this->actions);
    }

    /**
     * 执行一个搜索
     *
     */
    public function search(Request $request)
    {
        $data = $request::input();
//        dd($data);
        $lists = ActivityApply::with('Activity.ActivityType', 'Maker');

        if (isset($data['activity_name']) && $data['activity_name'] != '') {
            $aIds = Activity::where('subject', 'like', '%' . $data['activity_name'] . '%')->lists('id');
            $lists->whereIn('activity_id', $aIds);
        }

        if (isset($data['name']) && $data['name'] != '') {
            $lists->where('name', 'like', '%' . $data['name'] . '%');
        }

        if (isset($data['tel']) && $data['tel'] != '') {
            $lists->where('tel', 'like', '%' . $data['tel'] . '%');
        }

        $lists = $lists->paginate(10);

        return view('activityapply.search')->with("lists", $lists)->with("activity_name", $data['activity_name'])
            ->with("name", $data['name'])->with("tel", $data['tel'])->with("actions", $this->actions);
    }

    public function getDetail()
    {
        $aid = Request::input('aid');
//        $apply = DB::table('activity_apply as aa')->join("activity as a", "aa.activity_id", "=", "a.id")
//            ->join("activity_type as at", "a.type", "=", "at.code")->join("maker as m", "aa.maker_id", "=", "m.id")
//            ->join("zone as z", "aa.join_zone_id", "=", "z.id")
//            ->select('aa.*', 'a.subject as activity_subject', 'at.name as type_name', 'm.name as maker_name', 'z.name as zone_name')
//            ->where('aa.id', intval($aid))->first();

        $apply = ActivityApply::with('Maker', 'Activity.ActivityType', 'zone')->where('id', intval($aid))->first();


        return view('activityapply.detail')->with("apply", $apply);
    }

    /**
     *获取编辑页面
     */
    public function getEdit(Request $request){
        $data = Request::input();

        $list = ActivityApply::with('maker')->where('id', $data['id'])->first();

        return view('activityapply.edit')->with('list', $list);
    }

    /**
     *执行一个编辑
     */
    public function postEdit(Request $request)
    {
        $data = Request::input();
        unset($data['maker_search'], $data['formType'], $data['_token']);

        ActivityApply::where('id', $data['id'])->update($data);

        return AjaxCallbackMessage('编辑成功', true, url('activityapply/index'));
    }
}